package com.example.demo01.demo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import com.example.demo01.R;
import com.example.demo01.adapter.ListDemoAdapter;
import com.example.demo01.consts.UserData;
import com.example.demo01.entity.UserDemo;
import com.example.demo01.fragments.TitleFragment;
import com.example.demo01.utils.ToastUtil;

public class ListViewDemo extends AppCompatActivity {
    ToastUtil toastUtil = new ToastUtil(this);
    private ListView lv_list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_view);
        EdgeToEdge.enable(this);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        //. 添加标题
        getSupportFragmentManager()
                .beginTransaction()
                .add(R.id.fl_title, TitleFragment.newInstance(getResources().getString(R.string.ListViewDemo)))
                .commit();
        initView();
    }

    //. 初始化视图
    private void initView() {
        /// 获取控件
        lv_list = findViewById(R.id.lv_list);


        addListener();
    }

    //. 添加点击事件
    private void addListener() {
        /// 创建适配器
        ListDemoAdapter Adapter = new ListDemoAdapter(this, UserData.USER_DEMOS);
        /// 设置适配器
        lv_list.setAdapter(Adapter);
        /// 设置点击事件
        lv_list.setOnItemClickListener((parent, view, position, id) -> {
            UserDemo item = Adapter.getItem(position);
            Intent intent = new Intent(ListViewDemo.this, IntentTestPage.class);
            intent.putExtra("user", item);
            startActivity(intent);
        });
    }
}